package test;

import java.util.ArrayList;
import java.util.List;



public class ChinaUMSZjSecurity {
	public Object getConstruct() throws Exception {
		List<String> ipList = new ArrayList<String>();
//		ipList.add(0, MacConfig.macServerIp);
		List<Integer> portList = new ArrayList<Integer>();
//		portList.add(0, Integer.valueOf(MacConfig.macPort));
//		UnionEsscAPI shortApi = new UnionEsscAPI(ipList, portList, 7,FaceContext.zjys_sysID, FaceContext.zjys_appID);
//		return shortApi;
		return null;
	}
	
	public String getMacFromByte(byte [] src) throws Exception {
		String data = BitConvert.Hex2Str(splitAndXOR(src));
		
		String macValue = getMacValue(data);
		if (macValue == null) {
			throw new Exception("调用加密机计算MAC出错");
		}
		String actrulMac = BitConvert.Str2HexDecimal(macValue);
		
		return actrulMac;
	}

	/**
	 * 生成MAC值
	 * 
	 * @param mode
	 * @param keyName
	 * @param keyValue
	 * @param algorithmID
	 * @param fillMode
	 * 
	 @param dataType
	 * @param data
	 * @return
	 * @throws Exception 
	 */
	public  String getMacValue(String data) throws Exception {
//		UnionEsscAPI shortApi = getConstruct();
//		 TUnionTransInfo transInfo = shortApi.unionAPIServiceE150(1,
//				 MacConfig.zakName,
//				"",
//				Integer.parseInt(MacConfig.algorithmID),
//				1,
//				MacConfig.fillMode,
//				data);
//		if (transInfo.getIsSuccess() == 1) {
//			return transInfo.getReturnBody().getMac();
//		} else {
//			String log = null;
//			try {
//				log = new String(transInfo.getLog().getBytes(),"GBK");
//			} catch (Exception e) {
//			}
			return "0000000000000000";
//		}
	}
	
	public boolean chkMacValue(String macValue, byte[] src) throws Exception {
		String mac = getMacFromByte(src);
		if ( mac.equals(macValue)) {
			return true;
		}
		
		return false;
	}
	
	public TUnionTransInfo E112(String keyValue,String checkValue,int activeDate) throws Exception{
//		UnionEsscAPI shortApi = getConstruct();
//		System.out.println("加密方式（protectFlag）="+MacConfig.protectFlag);
//		return shortApi.unionAPIServiceE112(MacConfig.zakName, keyValue, checkValue, MacConfig.protectFlag, MacConfig.protectKey,activeDate);
		return null;
	}
	
	public byte[] splitAndXOR(byte[] data) {
		if (data == null) {
			return null;
		}

		int group = (data.length + (8 - 1)) / 8;
		int offset = 0;
		byte[] edata = null;
		for (int i = 0; i < group; i++) {
			byte[] temp = new byte[8];
			if (i != group - 1) {
				System.arraycopy(data, offset, temp, 0, 8);
				offset += 8;
			} else {// 只有最后一组数据才进行填充0x00
				System.arraycopy(data, offset, temp, 0, data.length - offset);
			}

			if (i != 0) {// 只有第一次不做异或
				edata = XOR(edata, temp);
			} else {
				edata = temp;
			}
		}
		return edata;
	}
	
	private byte[] XOR(byte[] data1, byte[] data2) {
		byte[] result = new byte[8];
		for (int i = 0, j = result.length; i < j; i++) {
			result[i] = (byte) (data1[i] ^ data2[i]);
		}
		return result;
	}
}
